package com.oneandseven.dajia.service.impl.album;

import com.oneandseven.dajia.base.service.impl.BaseService;
import com.oneandseven.dajia.constant.RedisKey;
import com.oneandseven.dajia.entity.album.AlbumInfoEntity;
import com.oneandseven.dajia.repository.album.IAlbumInfoRepository;
import com.oneandseven.dajia.service.album.IAlbumInfoService;
import com.oneandseven.dajia.vo.album.AlbumInfoVo;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AlbumInfoService extends BaseService<AlbumInfoEntity, IAlbumInfoRepository> implements IAlbumInfoService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public List<AlbumInfoVo> queryByTag(String tagCode) {
        List<AlbumInfoEntity> list = this.repository.queryByAlbumTagCode(tagCode);
        if(CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(albumInfoEntity -> {
            AlbumInfoVo albumInfoVo =new AlbumInfoVo(albumInfoEntity);
            String rewriteNums =  stringRedisTemplate.opsForValue().get(RedisKey.ALBUM_REWRITE_NUMS);
            albumInfoVo.setRewriteNums(StringUtils.isEmpty(rewriteNums)?0:Integer.parseInt(rewriteNums));
            return albumInfoVo;
        }).collect(Collectors.toList());
    }
}
